package fr.webeo.teambuilder.bo;

import org.jdom.Element;

/**
 * The player business object.
 * 
 * @author Sébastien Bordes
 * @version $Revision$ $Date$ $Name$
 * @since TeamBuilder 1.0
 */
public class Player implements Comparable<Player> {

    /** The XML element of this object. */
    private Element element;

    /** The first name of the player. */
    private String firstName;

    /** The last name of the player. */
    private String lastName;

    /** The email of the player. */
    private String mail;

    /** The opponent wished. */
    private String opponentPref;

    /** The ready staus of the player. */
    private boolean ready;

    /** The teamMate wished. */
    private String teamMatePref;

    /** The preference team of the user. */
    private String teamPref;

    /**
     * The default constructor.
     */
    public Player() {

        setElement(new Element("player"));

        this.ready = false;
        this.lastName = "";
        this.firstName = "";
        this.mail = "";
        this.teamPref = "";
    }

    /**
     * THe constructor used while reading the xml document.
     * 
     * @param e the XML element to read for init
     */
    public Player(Element e) {
        this.ready = false;
        this.lastName = "";
        this.firstName = "";
        this.mail = "";
        this.teamPref = "";

        setElement(e);

        this.ready = "true".equals(e.getAttributeValue("ready"));
        this.lastName = e.getAttributeValue("lastName");
        this.firstName = e.getAttributeValue("firstName");
        this.mail = e.getAttributeValue("mail");
        this.teamPref = e.getAttributeValue("teamPref");
        this.teamMatePref = e.getAttributeValue("teamMatePref");
    }

    /**
     * {@inheritDoc}
     */
    public int compareTo(Player p) {
        return toString().compareToIgnoreCase(p.toString());
    }

    /**
     * @return Returns the element.
     */
    public Element getElement() {
        return this.element;
    }

    /**
     * @return Returns the firstName.
     */
    public String getFirstName() {
        return this.firstName;
    }

    /**
     * @return the lastName, firstname and email of the player.
     */
    public String getFullName() {
        return (new StringBuilder(String.valueOf(toString()))).append(" - ").append(getMail()).toString();
    }

    /**
     * @return Returns the lastName.
     */
    public String getLastName() {
        return this.lastName;
    }

    /**
     * @return Returns the mail.
     */
    public String getMail() {
        return this.mail;
    }

    /**
     * @return Returns the opponentPref.
     */
    public String getOpponentPref() {
        return opponentPref;
    }

    /**
     * @return Returns the teamMatePref.
     */
    public String getTeamMatePref() {
        return this.teamMatePref;
    }

    /**
     * @return Returns the teamPref.
     */
    public String getTeamPref() {
        return this.teamPref;
    }

    /**
     * <p>
     * TODO To remove.
     * </p>
     * 
     * @return
     * @since TeamBuilder 1.0
     */
    public boolean isDansEquipeA() {
        return "Equipe A".equals(getTeamPref());
    }

    /**
     * <p>
     * TODO To remove.
     * </p>
     * 
     * @return
     * @since TeamBuilder 1.0
     */
    public boolean isDansEquipeB() {
        return "Equipe B".equals(getTeamPref());
    }

    public boolean isReady() {
        return this.ready;
    }

    /**
     * Chek the lastname, firstname and Email field.
     * 
     * @return true if the player is correctl filled.
     * 
     * @since TeamBuilder 1.0
     */
    public boolean isValid() {
        return !"".equals(getLastName()) && !"".equals(getFirstName()) && !"".equals(getMail());
    }

    /**
     * @param element The element to set.
     */
    public void setElement(Element element) {
        this.element = element;
    }

    /**
     * @param firstName The firstName to set.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
        getElement().setAttribute("firstName", firstName);
    }

    /**
     * @param lastName The lastName to set.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
        getElement().setAttribute("lastName", lastName);
    }

    /**
     * @param mail The mail to set.
     */
    public void setMail(String mail) {
        this.mail = mail;
        getElement().setAttribute("mail", mail);
    }

    /**
     * @param opponentPref The opponentPref to set.
     */
    public void setOpponentPref(String opponentPref) {
        this.opponentPref = opponentPref;
    }

    /**
     * @param ready The ready to set.
     */
    public void setReady(boolean ready) {
        this.ready = ready;
        getElement().setAttribute("ready", ready ? "true" : "false");
    }

    /**
     * @param teamMatePref The teamMatePref to set.
     */
    public void setTeamMatePref(String teamMatePref) {
        if (teamMatePref == null) {
            this.teamMatePref = "";
        }
        this.teamMatePref = teamMatePref;
        getElement().setAttribute("teamMatePref", teamMatePref);
    }

    /**
     * @param teamPref The teamPref to set.
     */
    public void setTeamPref(String teamPref) {
        if (teamPref == null) {
            this.teamPref = "";
        }
        this.teamPref = teamPref;
        getElement().setAttribute("teamPref", teamPref);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return (new StringBuilder(String.valueOf(getLastName()))).append(" ").append(getFirstName()).toString();
    }

}
